home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / scm / wb1a1.lha / wb / test2.scm < prev    next >
Encoding:
Text File  |  1993-06-29  |  5.7 KB  |  154 lines

  1. ; Wb-tree File Based Associative String Data Base System.
  2. ; Copyright (c) 1991, 1992, 1993 Holland Mark Martin
  3. ;
  4. ;Permission to use, copy, modify, and distribute this software and its
  5. ;documentation for educational, research, and non-profit purposes and
  6. ;without fee is hereby granted, provided that the above copyright
  7. ;notice appear in all copies and that both that copyright notice and
  8. ;this permission notice appear in supporting documentation, and that
  9. ;the name of Holland Mark Martin not be used in advertising or
  10. ;publicity pertaining to distribution of the software without specific,
  11. ;written prior consent in each case.  Permission to incorporate this
  12. ;software into commercial products can be obtained from Jonathan
  13. ;Finger, Holland Mark Martin, 174 Middlesex Turnpike, Burlington, MA,
  14. ;01803-4467, USA.  Holland Mark Martin makes no representations about
  15. ;the suitability or correctness of this software for any purpose.  It
  16. ;is provided "as is" without express or implied warranty.  Holland Mark
  17. ;Martin is under no obligation to provide any services, by way of
  18. ;maintenance, update, or otherwise.
  19.  
  20. (require 'stdio)
  21. (require (in-vicinity (program-vicinity) "defs"))
  22.  
  23. ;; FUNC is called with (keystr klen vstr vlen user-arg)
  24. ;; FUNC can return SUCCESS for DELETE, NOTPRES/NOTDONE for SKIP,
  25. ;; TERMINATE to terminate the scan (no skip), RETRYERR to stop here
  26. ;; resumably (also no skip), or some real error code.
  27.  
  28. ;; SCAN-FUNC-1 always returns SUCCESS;
  29. ;; SCAN-FUNC-2 returns SUCCESS on every other call, except that if the
  30. ;;             key begins with "7" it return TERMINATE;
  31. ;; SCAN-FUNC-3 sets value to "1" of value starts with "0", and
  32. ;;;            "0" otherwise; it always returns a length of 1;
  33. ;;; SCAN-FUNC-4 sets the value to "abc...xyz" and returns SUCCESS (>=0)
  34. ;;;            half the time; else NOTPRES.
  35.  
  36. (define (scan-func-1 keystr klen vstr vlen user-arg)
  37.   (if user-arg
  38.       (fprintf diagout "SCAN-FUNC-1 called klen=%d vlen=%d, key='%.*s', val='%.*s'\\n"
  39.            klen vlen
  40.            klen keystr vlen vstr))
  41.   SUCCESS)
  42.  
  43. (define xxx #t)
  44.  
  45. (define (scan-func-2 keystr klen vstr vlen user-arg)
  46.   (set! xxx (not xxx))
  47.   (let ((result (if (char=? (string-ref keystr 0) (string-ref "7" 0))
  48.             TERMINATED
  49.             (if xxx SUCCESS NOTPRES))))
  50.     (fprintf diagout "SCAN-FUNC-2 called klen=%d vlen=%d, key='%.*s', val='%.*s' result=%d\\n"
  51.          klen vlen
  52.          klen keystr vlen vstr result)
  53.     result))
  54.  
  55. (define (scan-func-3 keystr klen vstr vlen user-arg)
  56.   (fprintf diagout "SCAN-FUNC-3 called klen=%d vlen=%d, key='%.*s', val='%.*s'\\n"
  57.        klen vlen
  58.        klen keystr vlen vstr)
  59.   (if (and (> vlen 0) (char=? (string-ref vstr 0) #\0))
  60.       (string-set! vstr 0 #\1)
  61.       (string-set! vstr 0 #\0))
  62.   1)
  63.  
  64. (define (scan-func-4 keystr klen vstr vlen user-arg)
  65.   (set! xxx (not xxx))
  66.   (let ((result (if (char=? (string-ref keystr 0) (string-ref "7" 0))
  67.             TERMINATED
  68.             (if xxx 26 NOTPRES))))
  69.     (fprintf diagout "SCAN-FUNC-4 called klen=%d vlen=%d, key='%.*s', val='%.*s' result=%d\\n"
  70.          klen vlen
  71.          klen keystr vlen vstr result)
  72.     (if (> result -1)
  73.     (substring-move! "abcdefghijklmnopqrstuvwxyz" 0 26 vstr 0))
  74.     result))
  75.  
  76. ;;; test functions
  77. ; this function both adds and removes in non-optimal orders
  78.  
  79. (define (fl-test4)
  80.   (set! buf-verbose #f)
  81.   (cstats)
  82.   (radd! 100 "abcdefghijklmnop")
  83.   (cstats) (show-buffers)
  84.   (remove! 1000 999 -1)
  85.   (cstats) (show-buffers)
  86.   (radd! 100 "abcdefghijklmnop")
  87.   (cstats) (show-buffers)
  88.   (remove! 1000 1099 -1)
  89.   (cstats) (show-buffers)
  90.   )
  91.  
  92. (define (scan-range han key-str k-len key2-str k2-len func user-arg)
  93.   (scan-internal COUNT-SCAN han key-str k-len key2-str k2-len func user-arg #t))
  94.  
  95. (define (delete-range han key-str k-len key2-str k2-len func user-arg)
  96.   (scan-internal REM-SCAN han key-str k-len key2-str k2-len func user-arg #t))
  97.  
  98. (define (update-range han key-str k-len key2-str k2-len func user-arg)
  99.   (scan-internal MODIFY-SCAN han key-str k-len key2-str k2-len func user-arg #t))
  100.  
  101. (define (scan-internal operation han key-str k-len key2-str k2-len func user-arg verbose)
  102.   (let ((respkt (make-vector PKT-SIZE))
  103.     (xstr (make-string 256))
  104.     (result #f))
  105.     (SET-SKEY-COUNT! respkt 0)
  106.     (if (>= k-len 0) (substring-move! key-str 0 k-len xstr 0))
  107.     (set! result (bt-scan han operation xstr k-len key2-str k2-len
  108.               func user-arg respkt 999))
  109.     (if verbose
  110.     (fprintf diagout
  111.          "SCAN(%s): found %d keys, result=%d, klen=%d, retkey='%.*s'\\n"
  112.          (cond ((eq? operation COUNT-SCAN) "COUNT")
  113.            ((eq? operation MODIFY-SCAN) "MODIFY")
  114.            ((not func) "UDELETE")
  115.            (else "DELETE"))
  116.          (KEY-COUNT respkt) result (KEY-LEN respkt)
  117.          (KEY-LEN respkt) xstr))
  118.     (if (= result SUCCESS) (KEY-COUNT respkt) result)))
  119.  
  120. (define (scount)
  121.   (fprintf diagout "**** %d items in range. **** \\n"
  122.          (scan-internal COUNT-SCAN current-bt "" -2 "" -1 #f #f #f)))
  123.  
  124. (define foostr "67")
  125.  
  126. ;; NOTE: the expected numbers of records for each (SCOUNT) are:
  127. ;; 5, 105, 86, 67, 58,... (If rerun, first count will be 58 not 5.)
  128.  
  129. (define (scan-test )
  130.   (scan-range current-bt "a" 1 "z" 1 scan-func-1 #f)
  131.   (scount)
  132.   (add! 100 1 1 "abcdefg")
  133.   (scan-range current-bt "a" 1 "z" 1 scan-func-1 #f)
  134.   (scan-range current-bt "a" 1 "z" 1 scan-func-2 #f)
  135.   (scount)
  136.   (delete-range current-bt "16" 2 "33" 2 scan-func-1 #f)
  137. ;  (scanf current-bt)
  138.   (scount)
  139.   (delete-range current-bt "1" 1 "43" 2 #f #f)
  140.   (scount)
  141.   (delete-range current-bt "43" 2 "59" 2 scan-func-2 #f)
  142.   (scount)
  143.   (scan-range current-bt foostr 2 "753" 3 scan-func-2 #f) ; test TERMINATE at "7"
  144.   (scount)
  145.   (update-range current-bt "74" 2 "94" 2 scan-func-3 #f)
  146.   (scount)
  147.   (update-range current-bt "84" 2 "99" 2 scan-func-4 #f)
  148.   (scount)
  149.   (update-range current-bt "84" 2 "94" 2 scan-func-3 #f)
  150.   (update-range current-bt "20" 2 "94" 2 scan-func-4 #f)
  151.   (scount)
  152.   (scan-range current-bt "" -2 "" -1 scan-func-1 #t)
  153.   )
  154.